package com.toantm.counthourswork;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.example.testproject.R;

import android.app.Activity;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

	private TextView mTxtResult = null;
	private TextView mTxtEror = null;
	private EditText mEdtData = null;
	private Button mBtn = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mTxtResult = (TextView) findViewById(R.id.activity_main_textview_result);
		mTxtEror = (TextView) findViewById(R.id.activity_main_textview_error);
		mEdtData = (EditText) findViewById(R.id.activity_main_edittext);
		mBtn = (Button) findViewById(R.id.activity_main_button);
		mBtn.setOnClickListener(this);
		// caculator();
	}

	private void caculator() {
		String result = null;
		float total = 0;
		int numError = 0;
		try {
			do {
				InputStream is = null;
				BufferedReader reader = null;
				is = getResources().openRawResource(R.raw.data);
				reader = new BufferedReader(new InputStreamReader(is));
				String readLine = null;
				while ((readLine = reader.readLine()) != null) {
					try {
						while (readLine.contains("  ")) {
							readLine = readLine.replaceAll("  ", " ");
						}
						while (readLine.endsWith(" ") || readLine.endsWith("\t")) {
							readLine = readLine.substring(0, readLine.length() - 1);
						}
						if (readLine.endsWith("2014") || readLine.endsWith("Chưa xác nhận")) {
							continue;
						}
						String[] arr = readLine.split("/");
						String[] arr2 = (arr[arr.length - 2]).split(" ");
						float current = Float.parseFloat(arr2[arr2.length - 1]);
						total += current;
					} catch (Exception e) {
						numError++;
					}
				}
				result = "Tổng ca làm của pé là:" + total;
			} while (false);
		} catch (Exception e) {
			e.printStackTrace();
			total = 0;
			result = "Có lỗi xảy ra: " + e.getMessage();
		}
		mTxtResult.setText(result);
		mTxtEror.setText(String.format("Có %d dòng lỗi", numError));
	}

	private void caculator(String data) {
		String result = null;
		float total = 0;
		int numError = 0;
		try {
			do {
				String[] arrData = data.split("\n");
				if (arrData == null || arrData.length == 0) {
					break;
				}
				for (String readLine : arrData) {
					try {
						while (readLine.contains("  ")) {
							readLine = readLine.replaceAll("  ", " ");
						}
						while (readLine.endsWith(" ") || readLine.endsWith("\t")) {
							readLine = readLine.substring(0, readLine.length() - 1);
						}
						if (readLine.endsWith("2014") || readLine.endsWith("Chưa xác nhận")) {
							continue;
						}
						String[] arr = readLine.split("/");
						String[] arr2 = (arr[arr.length - 2]).split(" ");
						float current = Float.parseFloat(arr2[arr2.length - 1]);
						total += current;
					} catch (Exception e) {
						numError++;
					}
				}
				result = "Tổng ca làm của pé là: " + total;
			} while (false);
		} catch (Exception e) {
			e.printStackTrace();
			total = 0;
			result = "Có lỗi xảy ra: " + e.getMessage();
		}
		mTxtResult.setText(result);
		mTxtEror.setText(String.format("Có %d dòng lỗi", numError));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View arg0) {
		ClipboardManager clipboard = (ClipboardManager) getApplicationContext().getSystemService(
				Context.CLIPBOARD_SERVICE);
		String data = clipboard.getPrimaryClip().getItemAt(0).getText().toString();
		if (data == null || data.equals("")) {
			Toast.makeText(getApplicationContext(), "Chưa có dữ liệu pé ơi...", Toast.LENGTH_LONG).show();
		} else {
			caculator(data);
		}
	}
}
